/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package buildings;

import static constants.ShipNumbers.*;
import static constants.ShipBuildTimes.*;

/**
 *
 * @author steely
 */
public class Shipyard {

    private static Boolean nanorobots = false;

    private int level;
    private int amountToBuild;
    private int totalBuildTime;
    private int buildTime;

    public Shipyard(int level, int ship) {
	this.level = level;
	amountToBuild = 0;
	totalBuildTime = 0;
	buildTime = calculateBuildTime(ship);
    }

    private int calculateBuildTime(int ship) {
	double buildSpeed;
	buildSpeed = nanorobots ? Math.pow(1.2, (level - 1)) * 1.3 : Math.pow(1.2, (level - 1));
	int baseBuildTime = 0;
	switch (ship) {
	    case FIGHTER:
		baseBuildTime = BASE_FIGHTER_BUILD_TIME;
		break;
	    case UNMANNED_PLATFORM:
		baseBuildTime = BASE_UNMANNED_PLATFORM_BUILD_TIME;
		break;
	    case UNMANNED_PLATFORM_MOD_2:
		baseBuildTime = BASE_UNMANNED_PLATFORM_MOD_2_BUILD_TIME;
		break;
	    case COLONIAL_SPACESHIP:
		baseBuildTime = BASE_COLONIAL_SPACESHIP_BUILD_TIME;
		break;
	    case ENGINERING_SPACESHIP:
		baseBuildTime = BASE_ENGINEERING_SPACESHIP_BUILD_TIME;
		break;
	    case FRIGATE:
		baseBuildTime = BASE_FRIGATE_BUILD_TIME;
		break;
	    case CORVETTE:
		baseBuildTime = BASE_CORVETTE_BUILD_TIME;
		break;

	}
	return (int) (Math.round(baseBuildTime / buildSpeed));
    }
    
    public static void setNanorobots(Boolean nanorobots) {
	Shipyard.nanorobots = nanorobots;
    }

    public int getLevel() {
	return level;
    }

    public int getAmountToBuild() {
	return amountToBuild;
    }

    public int getTotalBuildTime() {
	return totalBuildTime;
    }

    public int getBuildTime() {
	return buildTime;
    }

    public void buildAnother() {
	amountToBuild++;
	totalBuildTime += buildTime;
    }

    public void printInfo() {
	System.out.println("Build " + amountToBuild + " corvettes with your level " + level + " shipyard.");
	System.out.println("Build time: " + convertTime(totalBuildTime));
    }

    private String convertTime(int inSeconds) {
	int days = (int) (Math.floor(inSeconds / 86400));
	int hours = (int) (Math.floor(inSeconds / 3600) % 24);
	int minutes = (int) (Math.floor(inSeconds / 60) % 60);
	int seconds = inSeconds % 60;
	String formattedTime = "";
	int var = 0;

	if (days > 0) {
	    var++;
	}
	if (hours > 0) {
	    var++;
	}
	if (minutes > 0) {
	    var++;
	}
	if (seconds > 0) {
	    var++;
	}
	if (var == 2) {
	    if (days > 0) {
		var--;
		formattedTime = formattedTime + days + " " + checkPlural("day", days) + " and ";
	    }
	    if (hours > 0) {
		var--;
		formattedTime = var == 1 ? formattedTime + hours + " " + checkPlural("hour", hours) + " and " : formattedTime + hours + checkPlural("hour", hours) + ".";
	    }
	    if (minutes > 0) {
		var--;
		formattedTime = var == 1 ? formattedTime + minutes + " " + checkPlural("minute", minutes) + " and " : formattedTime + minutes + checkPlural("minute", minutes) + ".";
	    }
	    if (seconds > 0) {
		formattedTime = formattedTime + seconds + " " + checkPlural("second", seconds) + ".";
	    }
	} else {
	    if (days > 0) {
		var--;
		formattedTime = formattedTime + days + " " + checkPlural("day", days) + checkTimeline(var);
	    }
	    if (hours > 0) {
		var--;
		formattedTime = formattedTime + hours + " " + checkPlural("hour", hours) + checkTimeline(var);
	    }
	    if (minutes > 0) {
		var--;
		formattedTime = formattedTime + minutes + " " + checkPlural("minute", minutes) + checkTimeline(var);
	    }
	    if (seconds > 0) {
		formattedTime = formattedTime + seconds + " " + checkPlural("second", seconds) + ".";
	    }
	}

	return formattedTime;
    }

    // checks for what puntuation is needed
    private String checkTimeline(int var) {
	String addition;
	if (var == 0) {
	    addition = ".";
	} else if (var == 1) {
	    addition = ", and ";
	} else {
	    addition = ", ";
	}
	return addition;
    }

    private String checkPlural(String measurement, int amount) {
	if (measurement.contains("day")) {
	    return amount == 1 ? "day" : "days";
	} else if (measurement.contains("hour")) {
	    return amount == 1 ? "hour" : "hours";
	} else if (measurement.contains("minute")) {
	    return amount == 1 ? "minute" : "minutes";
	} else if (measurement.contains("second")) {
	    return amount == 1 ? "second" : "seconds";
	} else {
	    return "error";
	}
    }
}
